import { Provide, Inject } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository, In } from 'typeorm';
import { DPChangeSampleDetailCreateDto, DPChangeSampleDetailUpdateDto, DPChangeSampleDetailQueryDto, DPChangeSampleDetailDeleteDto } from '../dto/sampleDetial.dto';
import { TB_DPChangeSample_Detail } from '../entity/sampleDetail.entity';

@Provide()
export class DPChangeSampleDetailService {
  @InjectEntityModel(TB_DPChangeSample_Detail)
  sampleRepository: Repository<TB_DPChangeSample_Detail>;

  // 添加样本详情信息
  async addSampleDetail(params: DPChangeSampleDetailCreateDto) {
    try {
      let sampleDetail = new TB_DPChangeSample_Detail();
      sampleDetail.CHID = params.CHID;
      sampleDetail.NameA = params.NameA;
      sampleDetail.NameB = params.NameB;
      sampleDetail.NameL = params.NameL;
      sampleDetail.CreateTime = params.CreateTime;

      const result = await this.sampleRepository.save(sampleDetail);
      return {
        success: true,
        data: result,
        message: '新增样本详情成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '新增样本详情失败: ' + error.message,
      };
    }
  }

  // 获取所有样本详情信息
  async getAllSampleDetails() {
    try {
      const sampleDetails = await this.sampleRepository.find({
        order: {
          CreateTime: 'DESC',
        },
      });
      return {
        success: true,
        data: sampleDetails,
        message: '获取所有样本详情信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '获取所有样本详情信息失败: ' + error.message,
      };
    }
  }

  // 根据查询条件获取样本详情，支持分页
  async findSampleDetails(query: DPChangeSampleDetailQueryDto) {
    const { CHID, page = 1, limit = 10 } = query;

    const where: any = {};

    // 根据CHID查询
    if (CHID) {
      where['CHID'] = CHID;
    }

    try {
      const [samples, total] = await this.sampleRepository.findAndCount({
        where,
        skip: (page - 1) * limit,  // 跳过的条数
        take: limit,               // 每页返回的条数
      });

      return {
        success: true,
        data: samples,
        total,                      // 返回总条数
        message: '查询样本详情成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '查询样本详情失败: ' + error.message,
      };
    }
  }

  // 删除样本详情信息
  async deleteSampleDetail(id: number) {
    try {
      const sampleDetail = await this.sampleRepository.findOne({
        where: { OBJID: id },
      });
      if (!sampleDetail) {
        return {
          success: false,
          message: '要删除的样本详情不存在',
        };
      }

      await this.sampleRepository.remove(sampleDetail);
      return {
        success: true,
        message: '删除样本详情信息成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '删除样本详情信息失败: ' + error.message,
      };
    }
  }

  // 批量更新样本详情
  async batchUpdateSampleDetails(sampleIds: number[], updateData: DPChangeSampleDetailUpdateDto) {
    try {
      const sampleDetails = await this.sampleRepository.find({
        where: {
          OBJID: In(sampleIds),
        },
      });

      // 更新多个样本详情
      for (let sampleDetail of sampleDetails) {
        Object.assign(sampleDetail, updateData);
        await this.sampleRepository.save(sampleDetail);
      }

      return {
        success: true,
        message: '批量更新样本详情成功',
      };
    } catch (error) {
      return {
        success: false,
        message: '批量更新样本详情失败: ' + error.message,
      };
    }
  }
}
